package cn.edu.cqu.controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.cqu.domains.AcademicNews;
import cn.edu.cqu.domains.CollegeNews;
import cn.edu.cqu.domains.CollegeProfile;
import cn.edu.cqu.domains.Contact;
import cn.edu.cqu.domains.PartyNews;
import cn.edu.cqu.domains.StudyNews;
import cn.edu.cqu.domains.TeacherInfo;
import cn.edu.cqu.services.AcademicNewsCRUD;
import cn.edu.cqu.services.AdminCRUD;
import cn.edu.cqu.services.CollegeNewsCRUD;
import cn.edu.cqu.services.CollegeProfileCRUD;
import cn.edu.cqu.services.ContactCRUD;
import cn.edu.cqu.services.PartyNewsCRUD;
import cn.edu.cqu.services.StudyNewsCRUD;
import cn.edu.cqu.services.TeacherInfoCRUD;

@RestController
public class ContentController 
{
	@Autowired
	@Qualifier("AdminCRUDHibernate")
	private AdminCRUD adminCRUD;
	@Autowired
	@Qualifier("AcademicNewsCRUDHibernate")
	private AcademicNewsCRUD academicNewsCRUD ;
	@Autowired
	@Qualifier("CollegeNewsCRUDHibernate")
	private CollegeNewsCRUD collegeNewsCRUD;
	@Autowired
	@Qualifier("CollegeProfileCRUDHibernate")
	private CollegeProfileCRUD collegeProfileCRUD ;
	@Autowired
	@Qualifier("PartyNewsCRUDHibernate")
	private PartyNewsCRUD partyNewsCRUD ;
	@Autowired
	@Qualifier("ContactCRUDHibernate")
	private ContactCRUD contactCRUD;
	@Autowired
	@Qualifier("StudyNewsCRUDHibernate")
	private StudyNewsCRUD studyNewsCRUD;
	@Autowired
	@Qualifier("TeacherInfoCRUDHibernate")
	private TeacherInfoCRUD teacherInfoCRUD;
	
	/**
	 * Function:更新/插入具体新闻/内容
	 * @param contentType 类型
	 * @param subType 子类型
	 * @param id
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/updateContent.json") 
	public HashMap<String, Object> updateContent(
			@RequestParam(value="contentType",required=false) String contentType,
			@RequestParam(value="aID",required=false) String aID,
			@RequestParam(value="date",required=false) String date,
			@RequestParam(value="url",required=false) String url,
			@RequestParam(value="imgUrl",required=false) String imgUrl,
			@RequestParam(value="content",required=false) String content,
			@RequestParam(value="type",required=false) Integer type,
			@RequestParam(value="name",required=false) String name,
			@RequestParam(value="email",required=false) String email,
			@RequestParam(value="title",required=false) String title,
			@RequestParam(value="depatment",required=false) String depatment)
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
		System.out.println(contentType+"  " +
				content+"  " +
				name+"  " +
				email+"  " +
				title+"  " );
		
		//comment
//		String contentType = request.getParameter("contentType");
//		String aID = request.getParameter("aID");
//		String date = request.getParameter("date");
//		String url = request.getParameter("url");
//		String imgUrl = request.getParameter("imgUrl");
//		String content = request.getParameter("content");
//		Integer type = new Integer(request.getParameter("type"));
		//contact
//		String name = request.getParameter("name");
//		String email = request.getParameter("email");
//		String title = request.getParameter("title");
		//TeacherInfo
//		String depatment = request.getParameter("depatment");
		switch (contentType) {
		case "academicNews"://AcademicNews
			AcademicNews academicNews = new AcademicNews();
			academicNews.setAdmin(adminCRUD.getByID(aID));
			academicNews.setaNewsContent(content);
//			TODO
			academicNews.setaNewsDate(new Date(date));
			academicNews.setaNewsImgURL(imgUrl);
			academicNews.setaNewsTitle(title);
			academicNews.setaNewsURL(url);
			academicNewsCRUD.edit(academicNews);
			results.put("status", 0);
			break;
		case "collegeNews"://CollegeNews
			CollegeNews collegeNews = new CollegeNews();
			collegeNews.setAdmin(adminCRUD.getByID(aID));
			collegeNews.setcNewsContent(content);
//			TODO
			collegeNews.setcNewsDate(new Date(date));
			collegeNews.setcNewsImgURL(imgUrl);
			collegeNews.setcNewsTitle(title);
			collegeNews.setcNewsURL(url);
			collegeNewsCRUD.edit(collegeNews);
			results.put("status", 0);
			break;
		case "collegeProfile"://CollegeProfile
			CollegeProfile collegeProfile = new CollegeProfile();
			collegeProfile.setAdmin(adminCRUD.getByID(aID));
			collegeProfile.setCpContent(content);
			collegeProfile.setCpImgURL(imgUrl);
			collegeProfile.setCpTitle(title);
			collegeProfile.setCpURL(url);
			collegeProfileCRUD.edit(collegeProfile);
			results.put("status", 0);
			break;
		case "contact"://Contact
			Contact contact = new Contact();
			contact.setcContent(content);
			contact.setcEmail(email);
			contact.setcName(name);
			contact.setcTitle(title);
			contactCRUD.edit(contact);
			results.put("status", 0);
			break;
		case "partyNews"://PartyNews
			PartyNews partyNews = new PartyNews();
			partyNews.setAdmin(adminCRUD.getByID(aID));
			partyNews.setpNewsContent(content);
//			TODO
			partyNews.setpNewsDate(new Date(date));
			partyNews.setpNewsImgURL(imgUrl);
			partyNews.setpNewsTitle(title);
//			partyNews.setpNewsType(type);
			partyNews.setpNewsURL(url);
			partyNewsCRUD.edit(partyNews);
			results.put("status", 0);
			break;
		case "studyNews"://StudyNews
			StudyNews studyNews =  new StudyNews();
			studyNews.setAdmin(adminCRUD.getByID(aID));
			studyNews.setsNewsContent(content);
//			TODO
			studyNews.setsNewsDate(new Date(date));
			studyNews.setsNewsImgURL(imgUrl);
			studyNews.setsNewsTitle(title);
			studyNews.setsNewsType(type);
			studyNews.setsNewsURL(url);
			studyNewsCRUD.edit(studyNews);
			results.put("status", 0);
			break;
		case "teacherInfo"://TeacherInfo
			TeacherInfo teacherInfo = new TeacherInfo();
			teacherInfo.settContent(content);
			teacherInfo.settDepatment(depatment);
			teacherInfo.settImg(imgUrl);
			teacherInfo.settName(name);
			teacherInfo.settUrl(url);
			teacherInfoCRUD.edit(teacherInfo);
			results.put("status", 0);
			break;
		default:
			break;
		}
		return results;
	}
	/**
	 * Function:查询新闻/内容
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/selectContent.json") 
	public HashMap<String, Object> selectContent(
			@RequestParam(value="contentType",required=true) String contentType,
			@RequestParam(value="subType",required=false) Integer subType,
			@RequestParam(value="pageNo",required=true) Integer pageNo,
			@RequestParam(value="pageSize",required=true) Integer pageSize,
			@RequestParam(value="department",required=false) String department) throws UnsupportedEncodingException
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
		switch (contentType) {
		case "academicNews"://AcademicNews
			List<AcademicNews> academicNewsList = academicNewsCRUD.getAll(pageNo, pageSize);
			results.put("status", 0);
			results.put("academicNewsList", academicNewsList);
			break;
		case "collegeNews"://CollegeNews
			List<CollegeNews> collegeNewsList = collegeNewsCRUD.getAll(pageNo, pageSize);
			results.put("status", 0);
			results.put("collegeNewsList", collegeNewsList);
			break;
		case "collegeProfile"://CollegeProfile
			List<CollegeProfile> collegeProfileList = collegeProfileCRUD.getAll(pageNo, pageSize);
			results.put("status", 0);
			results.put("collegeProfileList", collegeProfileList);
			break;
		case "contact"://Contact
			List<Contact> contactList = contactCRUD.getAll(pageNo, pageSize);
			results.put("status", 0);
			results.put("contactList", contactList);
			break;
		case "partyNews"://PartyNews
			List<PartyNews> partyNewsList = new LinkedList<>();
//			switch (subType) {
//			case 0://all
				partyNewsList = partyNewsCRUD.getAll(pageNo, pageSize);
//				break;
//			default:
//				partyNewsList = partyNewsCRUD.getSubAll(subType, pageNo, pageSize);
//				break;
//			}
			results.put("status", 0);
			results.put("partyNewsList", partyNewsList);
			break;
		case "studyNews"://StudyNews
			List<StudyNews> studyNewsList = new LinkedList<>();
			switch (subType) {
			case 0://all
				studyNewsList = studyNewsCRUD.getAll(pageNo, pageSize);
				break;
			default:
				studyNewsList = studyNewsCRUD.getSubAll(subType, pageNo, pageSize);
				break;
			}
			results.put("status", 0);
			results.put("studyNewsList", studyNewsList);
			break;
		case "teacherInfo"://TeacherInfo
			List<TeacherInfo> teacherInfoList = new LinkedList<>();
			String depa = URLDecoder.decode(department,"UTF-8");
			teacherInfoList = teacherInfoCRUD.getByDepatment(depa,pageNo,pageSize);
			results.put("status", 0);
			results.put("teacherInfoList", teacherInfoList);
			break;
		}
		return results;	
	}

	/**
	 * Function:获取具体新闻/类型
	 * @param contentType 类型
	 * @param subType 子类型
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getContent.json") 
	public HashMap<String, Object> getContent(
			@RequestParam(value="contentType",required=true) String contentType,
			@RequestParam(value="id",required=true) String id)
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
//		String contentType = request.getParameter("contentType");
//		String id = request.getParameter("id");
		switch (contentType) {
		case "academicNews"://AcademicNews
			AcademicNews academicNews = academicNewsCRUD.getByID(id);
			results.put("status", 0);
			results.put("academicNews", academicNews);
			break;
		case "collegeNews"://CollegeNews
			CollegeNews collegeNews = collegeNewsCRUD.getByID(id);
			results.put("status", 0);
			results.put("collegeNews", collegeNews);
			break;
		case "collegeProfile"://CollegeProfile
			CollegeProfile collegeProfile = collegeProfileCRUD.getByID(id);
			results.put("status", 0);
			results.put("collegeProfile", collegeProfile);
			break;
		case "contact"://Contact
			Contact contact = contactCRUD.getByID(id);
			results.put("status", 0);
			results.put("contact", contact);
			break;
		case "partyNews"://PartyNews
			PartyNews partyNews = partyNewsCRUD.getByID(id);
			results.put("status", 0);
			results.put("partyNews", partyNews);
			break;
		case "studyNews"://StudyNews
			StudyNews studyNews =  studyNewsCRUD.getByID(id);
			results.put("status", 0);
			results.put("studyNews", studyNews);
			break;
		case "teacherInfo"://TeacherInfo
			TeacherInfo teacherInfo = teacherInfoCRUD.getByID(id);
			results.put("status", 0);
			results.put("teacherInfo", teacherInfo);
			break;
		default:
			break;
		}
		return results;
	}

	/**
	 * Function:删除新闻/内容
	 * @param contentType 类型
	 * @param subType 子类型
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteContent.json") 
	public HashMap<String, Object> deleteContent(
			@RequestParam(value="contentType",required=true) String contentType,
			@RequestParam(value="id",required=true) String id)
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
//		String contentType = request.getParameter("contentType");
//		String id = request.getParameter("id");
		switch (contentType) {
		case "academicNews"://AcademicNews
			academicNewsCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "collegeNews"://CollegeNews
			collegeNewsCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "collegeProfile"://CollegeProfile
			collegeProfileCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "contact"://Contact
			contactCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "partyNews"://PartyNews
			partyNewsCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "studyNews"://StudyNews
			studyNewsCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		case "teacherInfo"://TeacherInfo
			 teacherInfoCRUD.deleteByID(id);
			results.put("status", 0);
			break;
		default:
			break;
		}
		return results;
	}
}
